#ifndef _SESSION_BASE_H_
#define _SESSION_BASE_H_

#include "IoSocketEx.h"
#include "Header.h"

// 非gate的连接继承这个定义, 如各类非游戏服务器
class CSessionBase : public SGLib::CIoSocketEx
{
public:
    CSessionBase(s32 id);
    virtual ~CSessionBase();
    
    virtual void OnAccept();
    virtual void OnClose();
    virtual void OnRecv(const byte *pkg, s32 len);
    virtual SGLib::CIoSocketEx* Clone(s32 id);

    u64 GetSessionId();
    u64 GetServerId();
    bool IsAlive();

	bool SendMsg(u32 msgid, const IMessage &msg);
	bool SendMsgWithHead(u32 msgid, const IMessage &msg, char *head, int len);
    bool SendMsg(u32 msgid, const char *msg, s32 msglen);

    static s32 m_aliveInterval;
    static void SetAliveInterval(s32 interval){ m_aliveInterval = interval; }

protected:
	// 需要继承的接口
	virtual void _Clean();
	virtual void _OnMessage(u32 msgid, const byte *msg, s32 len){}
    void _SetSessionId(u64 id);

private:
    void _OnKeepAlive();
    void _OnServerRegister(const byte *pkg, s32 len);

protected:
    u64 m_sessionId;
    u64 m_serverId;
    s64 m_lastBeat;
    bool m_hasSendMessage;
};

#endif

